package com.example.demo.Controller;

import com.example.demo.Bean.*;
import com.example.demo.Service.ShopProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class ShopProductController {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ShopProductService shopProductService;

    @GetMapping("/getShopProduct")
    public List<ShopProduct> getShopProduct(String name){
        return shopProductService.getAllShopProduct(name);
    }

    @PostMapping("/deleteShop")
    public Boolean updateStock(Integer id,String category){
        DeleteParameter deleteParameter = new DeleteParameter();
        deleteParameter.setId(id);
        deleteParameter.setCategory(category);
        try{
            shopProductService.deleteShop(deleteParameter);
            return true;
            //这里还没做删除图片的操作
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @GetMapping("/getShopAllOrder")
    public List<ShopOrders> getShopAllOrder(String shopname){
        return shopProductService.getAllShopOrder(shopname);
    }

    @PostMapping("/changeShopData")
    public Boolean changeShopData(String shopname,String boss,String password,String address,String email){
        try{
            String md5 = shopname + password;
            byte[] bytes = md5.getBytes();
            String pwd = DigestUtils.md5DigestAsHex(bytes);
            ChangeShopData changeShopData = new ChangeShopData();
            changeShopData.setShopname(shopname);
            changeShopData.setPassword(pwd);
            changeShopData.setBoss(boss);
            changeShopData.setAddress(address);
            changeShopData.setEmail(email);
            shopProductService.changeShopData(changeShopData);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @PostMapping("insertCoupon")
    public Boolean insertCoupon(InsertCoupon insertCoupon){
        try{
            shopProductService.insertCoupon(insertCoupon);
            return true;
        }catch (Exception e){
            return  false;
        }
    }
}
